package clientCommunicator;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.ClassNotFoundException;
import java.net.Socket;
import java.net.UnknownHostException;
import common.CommandAnswer;


/**
 * ClientCommunicator - handle the communication between the server
 * 						the client send Command to server
 * 						by server interface, and wait for answer from server
  * @author Orit
 *
 */

public class ClientCommunicator implements Serializable{
	
	private static final long serialVersionUID = 1L;
	
	/**
	 * Default C'tor
	 */
	ClientCommunicator(){
	}
	/**
	 *  send Command to server and return the answer from server
	 * @param command to send
	 * @return CommandAnswer from server
	 */
    public CommandAnswer sendObjToServer(common.CommandExecutor command) {
    	 CommandAnswer answer = null;
    	try {
            // Create a connection to the server socket on the server application           
           /*
            * in our computers we use the local server ip.
            * in the company computers we use their ip:
            *  
            */
            Socket socket = new Socket("127.0.0.1", 7777);
    		//Socket socket = new Socket("10.1.1.200", 7777);
            // Send a message to the Server application
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(command);
      
            // Read and display the response message sent by server application
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            answer  = (CommandAnswer) ois.readObject();
            ois.close();
            oos.close();
            
        } catch (UnknownHostException e) { //catch any Exception that can be
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return answer;
    }
}